﻿using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace SDL
{
	public static class StringExtension
	{
		#region IsGuid
		private static readonly Regex _GuidRegEx = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");

		public static bool IsGuid(this string sString)
		{
			return (string.IsNullOrEmpty(sString) ? false : _GuidRegEx.IsMatch(sString));
		}
		#endregion

		#region IsNumeric
		private static readonly Regex _NumericRegEx = new Regex(@"^\d+(\.\d+)?$");

		public static bool IsNumeric(this string sString)
		{
			return (_NumericRegEx.IsMatch(sString));
		}
		#endregion

		public static ConnectionString ToConnection(this string sString)
		{
			return (ConnectionString.Parse(sString));
		}

        public static string MD5(this string input)
        {
            var x = new MD5CryptoServiceProvider();
            var bs = Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            var s = new StringBuilder();
            foreach (var b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            return(s.ToString());
        }
	}
}
